﻿using UnityEngine;
using System.Collections;
using Framework.Utils.SingletonTemplate;
using Game;
using System;
using System.Collections.Generic;

public class LanguageManager : MgrBase<LanguageManager>
{
    public LanguageType CurLanguage { get; private set; }

    public LanguageType SystemLanguage { get; private set; }

    private List<ILanguageText> _languageTexts;

    protected override void InitMge()
    {
        _languageTexts = new List<ILanguageText>();
        switch (Application.systemLanguage)
        {
            case UnityEngine.SystemLanguage.ChineseTraditional:
                SystemLanguage = LanguageType.ChineseTraditional;
                break;
            case UnityEngine.SystemLanguage.ChineseSimplified:
                SystemLanguage = LanguageType.ChineseSimplified;
                break;
            case UnityEngine.SystemLanguage.Chinese:
                SystemLanguage = LanguageType.ChineseSimplified;
                break;
            default:
                SystemLanguage = LanguageType.English;
                break;
        }
        CurLanguage = LanguageType.Auto;
    }
    public void SetLanguage(LanguageType language) 
    {
        if (CurLanguage != language)
        {
            CurLanguage = language;
            UpdateLanguageText();
        }
        
    }
    public RofLanguageRow GetLanguageDataByID(int id) 
    {
        return RofManager.Instance.RofLanguageTable.GetDataByID(id);
    }
    public RofLanguageRow GetLanguageData(string key)
    {
        if (key == null)
            return null;
        var table = RofManager.Instance.RofLanguageTable;
        for (int i = 0; i < table.RowNum; i++)
        {
            var data = table.GetDataByRow(i);
            if (data.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
            {
                return data;
            }
        }
        return null;
    }
    public string GetLanguageByID(int id) 
    {
        var data = RofManager.Instance.RofLanguageTable.GetDataByID(id);
        if (data != null)
        {
            return ToLanguageStr(data);
        }
        return null;
    }
    public string GetLanguage(string key) 
    {
        if (key == null)
            return null;
        var table = RofManager.Instance.RofLanguageTable;
        for (int i = 0; i < table.RowNum; i++)
        {
            var data = table.GetDataByRow(i);
            if (data.Key.Equals(key , StringComparison.OrdinalIgnoreCase))
            {
                return ToLanguageStr(data);
            }
        }
        return null;
    }
    public string ToLanguageStr(RofLanguageRow rofLanguage) 
    {
        var languageType = CurLanguage;
        if (CurLanguage == LanguageType.Auto)
        {
            languageType = SystemLanguage;
        }
        switch (languageType)
        {
            case LanguageType.ChineseSimplified:
                return rofLanguage.ChineseSimplified;
            case LanguageType.ChineseTraditional:
                return rofLanguage.ChineseTraditional;
            case LanguageType.English:
                return rofLanguage.English;
        }
        return null;
    }

    internal void Regist(ILanguageText languageText) 
    {
        _languageTexts.Add(languageText);
    }

    internal void UnRegist(ILanguageText languageText) 
    {
        _languageTexts.Remove(languageText);
    }

    private void UpdateLanguageText() 
    {
        for (int i = 0; i < _languageTexts.Count; i++)
        {
            _languageTexts[i].UpdateText();
        }
    }
}
